﻿// dev.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"

#include <iostream>
#include <windows.h>
//#define MZ_COMPAT_VERSION 302
//#define HAVE_COMPAT
//
//#include "minizip-ng/mz.h"
//#ifdef HAVE_COMPAT
//#include "minizip-ng/mz_compat.h"
//#endif
//#include "minizip-ng/mz_crypt.h"
//#include "minizip-ng/mz_os.h"
//#include "minizip-ng/mz_strm.h"
//#ifdef HAVE_BZIP2
//#include "minizip-ng/mz_strm_bzip.h"
//#endif
//#ifdef HAVE_PKCRYPT
//#include "minizip-ng/mz_strm_pkcrypt.h"
//#endif
//#include "minizip-ng/mz_strm_mem.h"
//#include "minizip-ng/mz_strm_os.h"
//#ifdef HAVE_WZAES
//#include "minizip-ng/mz_strm_wzaes.h"
//#endif
//#ifdef HAVE_ZLIB
//#include "minizip-ng/mz_strm_zlib.h"
//#endif
//#include "minizip-ng/mz_zip.h"

int32_t test_zip_compat_int(zipFile zip,const char* filename)
{
    int32_t err = ZIP_OK;
    zip_fileinfo file_info;
    const char* buffer = "test data";
        
    memset(&file_info, 0, sizeof(file_info));

    FILETIME file_time{};
    //uint32_t dos_time;
    //FileTimeToDosDateTime(&file_time, &dos_time，&dos_time);
    
    file_info.dos_date = mz_zip_time_t_to_dos_date(1588561637);
  
    err = zipOpenNewFileInZip(zip, filename, &file_info, NULL, 0, NULL, 0, "test local comment",
        Z_DEFLATED, 1);
    if (err != ZIP_OK)
    {
        printf("Failed to create new file in zip (%" PRId32 ")\n", err);
        return err;
    }
    err = zipWriteInFileInZip(zip, buffer, (uint32_t)strlen(buffer));
    if (err != ZIP_OK)
    {
        printf("Failed to write file in zip (%" PRId32 ")\n", err);
        return err;
    }
    err = zipCloseFileInZip(zip);
    if (err != ZIP_OK)
    {
        printf("Failed to close file in zip (%" PRId32 ")\n", err);
        return err;
    }

    return ZIP_OK;
}

int32_t test_zip_compat(void)
{
    int32_t err = ZIP_OK;
    zipFile zip;


    zip = zipOpen("compat.zip", APPEND_STATUS_CREATE);

    if (zip == NULL)
    {
        printf("Failed to create test zip file\n");
        return ZIP_PARAMERROR;
    }
    err = test_zip_compat_int(zip, "test.txt");
    if (err != ZIP_OK)
        return err;
    err = test_zip_compat_int(zip, "test2.txt");
    if (err != ZIP_OK)
        return err;

    zipClose(zip, "test global comment");

    if (err != ZIP_OK)
        return err;

    printf("Compat zip.. OK\n");

    return ZIP_OK;
}

template<class T>
inline void* zeromem(T& p) {
    int n = sizeof(T);
    return memset(&p, 0,n);
}

static int32_t test_unzip_compat_int(unzFile unzip)
{
    unz_global_info64 global_info64;
    unz_global_info global_info;
    unz_file_info64 file_info64;
    unz_file_info file_info;
    unz_file_pos file_pos;
    int32_t err = UNZ_OK;
    int32_t bytes_read = 0;
    char comment[120];
    char filename[120];
    char buffer[120];
    const char* test_data = "test data";

    zeromem(global_info64);
    memset(&file_info, 0, sizeof(file_info));
    memset(&file_info64, 0, sizeof(file_info64));
    memset(&global_info, 0, sizeof(global_info));
    memset(&global_info64, 0, sizeof(global_info64));

    comment[0] = 0;
    err = unzGetGlobalComment(unzip, comment, sizeof(comment));
    if (err != UNZ_OK)
    {
        printf("Failed to get global comment (%" PRId32 ")\n", err);
        return err;
    }
    if (strcmp(comment, "test global comment") != 0)
    {
        printf("Unexpected global comment value (%s)\n", comment);
        return err;
    }
    err = unzGetGlobalInfo(unzip, &global_info);
    if (err != UNZ_OK)
    {
        printf("Failed to get global info  (%" PRId32 ")\n", err);
        return err;
    }
    err = unzGetGlobalInfo64(unzip, &global_info64);
    if (err != UNZ_OK)
    {
        printf("Failed to get global info 64-bit (%" PRId32 ")\n", err);
        return err;
    }
    if (global_info.number_entry != 2 || global_info64.number_entry != 2)
    {
        printf("Invalid number of entries in zip (%" PRId32 ")\n", global_info.number_entry);
        return err;
    }
    if (global_info.number_disk_with_CD != 0 || global_info64.number_disk_with_CD != 0)
    {
        printf("Invalid disk with cd (%" PRIu32 ")\n", global_info.number_disk_with_CD);
        return err;
    }
    
    err = unzLocateFile(unzip, "test.txt", (unzFileNameComparer)(void*)1);
    if (err != UNZ_OK)
    {
        printf("Failed to locate test file (%" PRId32 ")\n", err);
        return err;
    }

    err = unzGoToFirstFile(unzip);
    if (err == UNZ_OK)
    {
        filename[0] = 0;
        err = unzGetCurrentFileInfo64(unzip, &file_info64, filename, sizeof(filename), NULL, 0, NULL, 0);
        if (err != UNZ_OK)
        {
            printf("Failed to get current file info 64-bit (%" PRId32 ")\n", err);
            return err;
        }

        err = unzOpenCurrentFile(unzip);
        if (err != UNZ_OK)
        {
            printf("Failed to open current file (%" PRId32 ")\n", err);
            return err;
        }
        bytes_read = unzReadCurrentFile(unzip, buffer, sizeof(buffer));
        if (bytes_read != (int32_t)strlen(test_data))
        {
            printf("Failed to read zip entry data (%" PRId32 ")\n", err);
            unzCloseCurrentFile(unzip);
            return err;
        }
        if (unzEndOfFile(unzip) != 1)
        {
            printf("End of unzip not reported correctly\n");
            return UNZ_INTERNALERROR;
        }
        err = unzCloseCurrentFile(unzip);
        if (err != UNZ_OK)
        {
            printf("Failed to close current file (%" PRId32 ")\n", err);
            return err;
        }

        if (unztell(unzip) != bytes_read)
        {
            printf("Unzip position not reported correctly\n");
            return UNZ_INTERNALERROR;
        }

        err = unzGoToNextFile(unzip);
        if (err != UNZ_OK)
        {
            printf("Failed to get next file info (%" PRId32 ")\n", err);
            return err;
        }

        comment[0] = 0;
        err = unzGetCurrentFileInfo(unzip, &file_info, filename, sizeof(filename), NULL, 0, comment, sizeof(comment));
        if (err != UNZ_OK)
        {
            printf("Failed to get current file info (%" PRId32 ")\n", err);
            return err;
        }
        if (strcmp(comment, "test local comment") != 0)
        {
            printf("Unexpected local comment value (%s)\n", comment);
            return err;
        }

        err = unzGetFilePos(unzip, &file_pos);
        if (err != UNZ_OK)
        {
            printf("Failed to get file position (%" PRId32 ")\n", err);
            return err;
        }
        if (file_pos.num_of_file != 1)
        {
            printf("Unzip file position not reported correctly\n");
            return UNZ_INTERNALERROR;
        }

        err = unzGetOffset(unzip);
        if (err <= 0)
        {
            printf("Unzip invalid offset reported\n");
            return UNZ_INTERNALERROR;
        }

        err = unzGoToNextFile(unzip);

        if (err != UNZ_END_OF_LIST_OF_FILE)
        {
            printf("Failed to reach end of zip entries (%" PRId32 ")\n", err);
            unzCloseCurrentFile(unzip);
            return err;
        }
        err = unzSeek64(unzip, 0, SEEK_SET);
    }

    return UNZ_OK;
}

int32_t test_unzip_compat(void)
{
    unzFile unzip;
    int32_t err = UNZ_OK;

    unzip = unzOpen("compat.zip");
    if (unzip == NULL)
    {
        printf("Failed to open test zip file\n");
        return UNZ_PARAMERROR;
    }
    err = test_unzip_compat_int(unzip);
    unzClose(unzip);

    if (err != UNZ_OK)
        return err;

    printf("Compat unzip.. OK\n");

    return UNZ_OK;
}


int main()
{
    test_zip_compat();
    test_unzip_compat();
    std::cout << "Hello World!\n";
    std::unique_ptr<int> uptr(new int(3));


}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
